home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / form / frm_def.c < prev    next >
C/C++ Source or Header  |  2002-10-24  |  13KB  |  383 lines

  1. /****************************************************************************
  2.  * Copyright (c) 1998,2000 Free Software Foundation, Inc.                   *
  3.  *                                                                          *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a  *
  5.  * copy of this software and associated documentation files (the            *
  6.  * "Software"), to deal in the Software without restriction, including      *
  7.  * without limitation the rights to use, copy, modify, merge, publish,      *
  8.  * distribute, distribute with modifications, sublicense, and/or sell       *
  9.  * copies of the Software, and to permit persons to whom the Software is    *
  10.  * furnished to do so, subject to the following conditions:                 *
  11.  *                                                                          *
  12.  * The above copyright notice and this permission notice shall be included  *
  13.  * in all copies or substantial portions of the Software.                   *
  14.  *                                                                          *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
  16.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
  17.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
  18.  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
  19.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
  20.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
  21.  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
  22.  *                                                                          *
  23.  * Except as contained in this notice, the name(s) of the above copyright   *
  24.  * holders shall not be used in advertising or otherwise to promote the     *
  25.  * sale, use or other dealings in this Software without prior written       *
  26.  * authorization.                                                           *
  27.  ****************************************************************************/
  28.  
  29. /****************************************************************************
  30.  *   Author:  Juergen Pfeifer, 1995,1997                                    *
  31.  *   Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en             *
  32.  ****************************************************************************/
  33.  
  34. #include "form.priv.h"
  35.  
  36. MODULE_ID("$Id: frm_def.c,v 1.11 2002/07/06 15:33:27 juergen Exp $")
  37.  
  38. /* this can't be readonly */
  39. static FORM default_form = {
  40.   0,                                    /* status     */
  41.   0,                                    /* rows       */
  42.   0,                                    /* cols       */
  43.   0,                                    /* currow     */
  44.   0,                                    /* curcol     */
  45.   0,                                    /* toprow     */
  46.   0,                                    /* begincol   */
  47.   -1,                                   /* maxfield   */
  48.   -1,                                   /* maxpage    */
  49.   -1,                                   /* curpage    */
  50.   ALL_FORM_OPTS,                        /* opts       */
  51.   (WINDOW *)0,                          /* win        */
  52.   (WINDOW *)0,                          /* sub        */
  53.   (WINDOW *)0,                          /* w          */
  54.   (FIELD **)0,                          /* field      */
  55.   (FIELD *)0,                           /* current    */
  56.   (_PAGE *)0,                           /* page       */
  57.   (char *)0,                            /* usrptr     */
  58.   NULL,                            /* forminit   */
  59.   NULL,                                 /* formterm   */
  60.   NULL,                                 /* fieldinit  */
  61.   NULL                                  /* fieldterm  */
  62. };
  63.  
  64. NCURSES_EXPORT_VAR(FORM *) _nc_Default_Form = &default_form;
  65.  
  66. /*---------------------------------------------------------------------------
  67. |   Facility      :  libnform  
  68. |   Function      :  static FIELD *Insert_Field_By_Position(
  69. |                                     FIELD *new_field, 
  70. |                                     FIELD *head )
  71. |   
  72. |   Description   :  Insert new_field into sorted fieldlist with head "head"
  73. |                    and return new head of sorted fieldlist. Sorting
  74. |                    criteria is (row,column). This is a circular list.
  75. |
  76. |   Return Values :  New head of sorted fieldlist
  77. +--------------------------------------------------------------------------*/
  78. static FIELD *Insert_Field_By_Position(FIELD *newfield, FIELD *head)
  79. {
  80.   FIELD *current, *newhead;
  81.   
  82.   assert(newfield);
  83.  
  84.   if (!head)
  85.     { /* empty list is trivial */
  86.       newhead = newfield->snext = newfield->sprev = newfield;
  87.     }
  88.   else
  89.     {
  90.       newhead = current = head;
  91.       while((current->frow < newfield->frow) || 
  92.         ((current->frow==newfield->frow) && 
  93.          (current->fcol < newfield->fcol)) )
  94.     {
  95.       current = current->snext;
  96.       if (current==head)
  97.         { /* We cycled through. Reset head to indicate that */
  98.           head = (FIELD *)0;
  99.           break;
  100.         }
  101.     }
  102.       /* we leave the loop with current pointing to the field after newfield*/
  103.       newfield->snext     = current;
  104.       newfield->sprev     = current->sprev;
  105.       newfield->snext->sprev = newfield;
  106.       newfield->sprev->snext = newfield;
  107.       if (current==head) 
  108.     newhead = newfield;
  109.     }
  110.   return(newhead);
  111. }
  112.  
  113. /*---------------------------------------------------------------------------
  114. |   Facility      :  libnform  
  115. |   Function      :  static void Disconnect_Fields(FORM *form)
  116. |   
  117. |   Description   :  Break association between form and array of fields.
  118. |
  119. |   Return Values :  -
  120. +--------------------------------------------------------------------------*/
  121. static void Disconnect_Fields( FORM * form )
  122. {
  123.   if (form->field)
  124.     {
  125.       FIELD **fields;
  126.  
  127.       for(fields=form->field;*fields;fields++)
  128.     {
  129.       if (form == (*fields)->form) 
  130.         (*fields)->form = (FORM *)0;
  131.     }
  132.       
  133.       form->rows = form->cols = 0;
  134.       form->maxfield = form->maxpage = -1;
  135.       form->field = (FIELD **)0;
  136.       if (form->page) 
  137.     free(form->page);
  138.       form->page = (_PAGE *)0;
  139.     }    
  140. }
  141.  
  142. /*---------------------------------------------------------------------------
  143. |   Facility      :  libnform  
  144. |   Function      :  static int Connect_Fields(FORM *form, FIELD **fields)
  145. |   
  146. |   Description   :  Set association between form and array of fields.
  147. |
  148. |   Return Values :  E_OK            - no error
  149. |                    E_CONNECTED     - a field is already connected
  150. |                    E_BAD_ARGUMENT  - Invalid form pointer or field array
  151. |                    E_SYSTEM_ERROR  - not enough memory
  152. +--------------------------------------------------------------------------*/
  153. static int Connect_Fields(FORM  * form, FIELD ** fields)
  154. {
  155.   int field_cnt, j;
  156.   int page_nr;
  157.   int maximum_row_in_field, maximum_col_in_field;
  158.   _PAGE *pg;
  159.   
  160.   assert(form);
  161.  
  162.   form->field    = fields;
  163.   form->maxfield = 0;
  164.   form->maxpage  = 0;
  165.  
  166.   if (!fields)
  167.     RETURN(E_OK);
  168.   
  169.   page_nr = 0;
  170.   /* store formpointer in fields and count pages */
  171.   for(field_cnt=0;fields[field_cnt];field_cnt++)
  172.     {
  173.       if (fields[field_cnt]->form) 
  174.     RETURN(E_CONNECTED);
  175.       if ( field_cnt==0 || 
  176.       (fields[field_cnt]->status & _NEWPAGE)) 
  177.     page_nr++;
  178.       fields[field_cnt]->form = form;
  179.     }    
  180.   if (field_cnt==0)
  181.     RETURN(E_BAD_ARGUMENT);
  182.   
  183.   /* allocate page structures */
  184.   if ( (pg = (_PAGE *)malloc(page_nr * sizeof(_PAGE))) != (_PAGE *)0 )
  185.     {
  186.       form->page = pg;
  187.     }
  188.   else
  189.     RETURN(E_SYSTEM_ERROR);
  190.   
  191.   /* Cycle through fields and calculate page boundaries as well as
  192.      size of the form */
  193.   for(j=0;j<field_cnt;j++)
  194.     {
  195.       if (j==0) 
  196.     pg->pmin = j;
  197.       else
  198.     {
  199.       if (fields[j]->status & _NEWPAGE)
  200.         {
  201.           pg->pmax = j-1;
  202.           pg++;
  203.           pg->pmin = j;
  204.         }
  205.     }
  206.       
  207.       maximum_row_in_field = fields[j]->frow + fields[j]->rows;
  208.       maximum_col_in_field = fields[j]->fcol + fields[j]->cols;
  209.       
  210.       if (form->rows < maximum_row_in_field) 
  211.     form->rows = maximum_row_in_field;
  212.       if (form->cols < maximum_col_in_field) 
  213.     form->cols = maximum_col_in_field;
  214.     }
  215.   
  216.   pg->pmax       = field_cnt-1;
  217.   form->maxfield = field_cnt;
  218.   form->maxpage  = page_nr; 
  219.   
  220.   /* Sort fields on form pages */
  221.   for(page_nr = 0;page_nr < form->maxpage; page_nr++)
  222.     {
  223.       FIELD *fld = (FIELD *)0;
  224.       for(j = form->page[page_nr].pmin;j <= form->page[page_nr].pmax;j++)
  225.     {
  226.       fields[j]->index = j;
  227.       fields[j]->page  = page_nr;
  228.       fld = Insert_Field_By_Position(fields[j],fld);
  229.     }
  230.       form->page[page_nr].smin = fld->index;
  231.       form->page[page_nr].smax = fld->sprev->index;
  232.     }
  233.   RETURN(E_OK);
  234. }
  235.  
  236. /*---------------------------------------------------------------------------
  237. |   Facility      :  libnform  
  238. |   Function      :  static int Associate_Fields(FORM *form, FIELD **fields)
  239. |   
  240. |   Description   :  Set association between form and array of fields. 
  241. |                    If there are fields, position to first active field.
  242. |
  243. |   Return Values :  E_OK            - success
  244. |                    any other       - error occured
  245. +--------------------------------------------------------------------------*/
  246. INLINE static int Associate_Fields(FORM  *form, FIELD **fields)
  247. {
  248.   int res = Connect_Fields(form,fields);
  249.   if (res == E_OK)
  250.     {
  251.       if (form->maxpage>0)
  252.     {
  253.       form->curpage = 0;
  254.       form_driver(form,FIRST_ACTIVE_MAGIC);
  255.     }
  256.       else
  257.     {
  258.       form->curpage = -1;
  259.       form->current = (FIELD *)0;
  260.     } 
  261.     }
  262.   return(res);
  263. }
  264.                 
  265. /*---------------------------------------------------------------------------
  266. |   Facility      :  libnform  
  267. |   Function      :  FORM *new_form( FIELD **fields )
  268. |   
  269. |   Description   :  Create new form with given array of fields.
  270. |
  271. |   Return Values :  Pointer to form. NULL if error occured.
  272. +--------------------------------------------------------------------------*/
  273. NCURSES_EXPORT(FORM *)
  274. new_form (FIELD ** fields)
  275. {    
  276.   int err = E_SYSTEM_ERROR;
  277.  
  278.   FORM *form = (FORM *)malloc(sizeof(FORM));
  279.   
  280.   if (form)
  281.     {
  282.       *form = *_nc_Default_Form;
  283.       if ((err=Associate_Fields(form,fields))!=E_OK)
  284.     {
  285.       free_form(form);
  286.       form = (FORM *)0;
  287.     }
  288.     }
  289.  
  290.   if (!form)
  291.     SET_ERROR(err);
  292.   
  293.   return(form);
  294. }
  295.  
  296. /*---------------------------------------------------------------------------
  297. |   Facility      :  libnform  
  298. |   Function      :  int free_form( FORM *form )
  299. |   
  300. |   Description   :  Release internal memory associated with form.
  301. |
  302. |   Return Values :  E_OK           - no error
  303. |                    E_BAD_ARGUMENT - invalid form pointer
  304. |                    E_POSTED       - form is posted
  305. +--------------------------------------------------------------------------*/
  306. NCURSES_EXPORT(int)
  307. free_form (FORM * form)
  308. {
  309.   if ( !form )    
  310.     RETURN(E_BAD_ARGUMENT);
  311.  
  312.   if ( form->status & _POSTED)  
  313.     RETURN(E_POSTED);
  314.   
  315.   Disconnect_Fields( form );
  316.   if (form->page) 
  317.     free(form->page);
  318.   free(form);
  319.   
  320.   RETURN(E_OK);
  321. }
  322.  
  323. /*---------------------------------------------------------------------------
  324. |   Facility      :  libnform  
  325. |   Function      :  int set_form_fields( FORM *form, FIELD **fields )
  326. |   
  327. |   Description   :  Set a new association of an array of fields to a form
  328. |
  329. |   Return Values :  E_OK              - no error
  330. |                    E_BAD_ARGUMENT    - invalid form pointer
  331. |                    E_POSTED          - form is posted
  332. +--------------------------------------------------------------------------*/
  333. NCURSES_EXPORT(int)
  334. set_form_fields (FORM  * form, FIELD ** fields)
  335. {
  336.   FIELD **old;
  337.   int res;
  338.   
  339.   if ( !form )    
  340.     RETURN(E_BAD_ARGUMENT);
  341.  
  342.   if ( form->status & _POSTED )    
  343.     RETURN(E_POSTED);
  344.   
  345.   old = form->field;
  346.   Disconnect_Fields( form );
  347.   
  348.   if( (res = Associate_Fields( form, fields )) != E_OK )
  349.     Connect_Fields( form, old );
  350.   
  351.   RETURN(res);
  352. }
  353.     
  354. /*---------------------------------------------------------------------------
  355. |   Facility      :  libnform  
  356. |   Function      :  FIELD **form_fields( const FORM *form )
  357. |   
  358. |   Description   :  Retrieve array of fields
  359. |
  360. |   Return Values :  Pointer to field array
  361. +--------------------------------------------------------------------------*/
  362. NCURSES_EXPORT(FIELD **)
  363. form_fields (const FORM * form)
  364. {
  365.   return (Normalize_Form( form )->field);
  366. }
  367.  
  368. /*---------------------------------------------------------------------------
  369. |   Facility      :  libnform  
  370. |   Function      :  int field_count( const FORM *form )
  371. |   
  372. |   Description   :  Retrieve number of fields
  373. |
  374. |   Return Values :  Number of fields, -1 if none are defined
  375. +--------------------------------------------------------------------------*/
  376. NCURSES_EXPORT(int)
  377. field_count (const FORM * form)
  378. {
  379.   return (Normalize_Form( form )->maxfield);
  380. }
  381.  
  382. /* frm_def.c ends here */
  383.